package com.uwo.house.controller;

import com.uwo.house.beans.request.HouseAddReq;
import com.uwo.house.beans.request.ModifySampleReq;
import com.uwo.house.beans.request.SampleHouseReq;
import com.uwo.house.beans.response.SampleHousesResp;
import com.uwo.house.service.ISampleHousesService;
import com.uwo.house.util.DateUtils;
import com.uwo.house.util.UtilReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 样板房管理中心Controller
 * Created by YZYG on 2017/7/19.
 */
@Controller
@RequestMapping("/sample")
public class SampleHousesController {
    @Autowired
    public ISampleHousesService sampleHousesService;

    /**
     * 管理员中心-样板房查询
     *
     * @param blockName
     * @param releaseDateFromStr
     * @param releaseDateToStr
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Map getSampleHouses(@RequestParam(value = "blockName", required = false) String blockName,
                               @RequestParam(value = "releaseDateFromStr", required = false) String releaseDateFromStr,
                               @RequestParam(value = "releaseDateToStr", required = false) String releaseDateToStr) {
        SampleHouseReq sampleHouseReq = new SampleHouseReq();
        sampleHouseReq.setBlockName(blockName);
        sampleHouseReq.setReleaseDateFrom(DateUtils.getDate(releaseDateFromStr));
        sampleHouseReq.setReleaseDateTo(DateUtils.getDate(releaseDateToStr));
        List<SampleHousesResp> list = this.sampleHousesService.getSampleHouses(sampleHouseReq);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }

    /**
     * 修改样板房信息
     *
     * @param modifySampleReq
     * @return
     */
    @RequestMapping(value = "/modify", method = RequestMethod.PUT)
    @ResponseBody
    public Map updateSampleHouses(@RequestBody ModifySampleReq modifySampleReq) {
        int result = this.sampleHousesService.updateSampleHouses(modifySampleReq);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "修改成功");
        }
        return UtilReturn.resErrorMap("修改失败！",UtilReturn.ERROR_CODE);
    }

    /**
     * 管理中心-删除样板房
     *
     * @param houseId
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public Map deleteSampleHouses(@RequestParam("houseId") int houseId) {
        int result = this.sampleHousesService.deleteSampleHouses(houseId);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "删除成功");
        }
        return UtilReturn.resErrorMap("删除失败！",UtilReturn.ERROR_CODE);
    }

    /**
     * 管理员中心-查询样板房详情（客户端通用）
     *
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public Map getSampleHouseById(@RequestParam("houseId") int houseId) {
        SampleHousesResp sampleHousesResp = this.sampleHousesService.getSampleHouseById(houseId);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, sampleHousesResp);
    }

    /**
     * 管理中心-发布样板房
     *
     * @param houseAddReq
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map addSampleHouses(@RequestBody HouseAddReq houseAddReq) {
        int result = this.sampleHousesService.addSampleHouses(houseAddReq);
        if (result == 1) {
            return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, "发布成功");
        }
        return UtilReturn.resErrorMap("发布失败！", UtilReturn.ERROR_CODE);
    }

    /**
     * 客户端条件查询样板房
     *
     * @param blockName
     * @param houseType
     * @param space
     * @param decorationStyle
     * @param areaSizeFrom
     * @param areaSizeTo
     * @param dealPriceFrom
     * @param dealPriceTo
     * @return
     */
    @RequestMapping(value = "/sample", method = RequestMethod.GET)
    @ResponseBody
    public Map getSampleHouses(@RequestParam(value = "province", required = false) String province,
                               @RequestParam(value = "city", required = false) String city,
                               @RequestParam(value = "blockName", required = false) String blockName,
                               @RequestParam(value = "houseType", required = false) String houseType,
                               @RequestParam(value = "space", required = false) String space,
                               @RequestParam(value = "decorationStyle", required = false) String decorationStyle,
                               @RequestParam(value = "areaSizeFrom", required = false) Double areaSizeFrom,
                               @RequestParam(value = "areaSizeTo", required = false) Double areaSizeTo,
                               @RequestParam(value = "dealPriceFrom", required = false) BigDecimal dealPriceFrom,
                               @RequestParam(value = "dealPriceTo", required = false) BigDecimal dealPriceTo,
                               @RequestParam(value = "searchKey", required = false) String searchKey) {
        List<SampleHousesResp> list = this.sampleHousesService.getSampleHousesByCondition(province,city,blockName, houseType, space, decorationStyle,
                areaSizeFrom, areaSizeTo, dealPriceFrom, dealPriceTo,searchKey);
        return UtilReturn.resSuccesssMap(UtilReturn.SUCCESS_CODE, UtilReturn.SUCCESS_MESSAGE, list);
    }
}
